package com.lsh.netty.simple;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/9/22 9:35 上午
 * @desc ： 客户端需要一个事件循环组
 */
public class NettyClient {

    public static void main(String[] args) throws InterruptedException {
        //客户端需要一个事件循环组
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
        //创建客户端启动对象  注意  创建的不是ServerBootstrap   而是BootStrap  netty包下的
        Bootstrap bootstrap = new Bootstrap();
         //设置相关参数
        bootstrap.group(group)//设置线程组
                .channel(NioSocketChannel.class)//设置客户端通道实现类
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new NettyClinetHandler());
                    }
                });
        System.out.println("客户端 is OK~");
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6666).sync();
        channelFuture.channel().closeFuture().sync();
        }finally {
            group.shutdownGracefully();
        }
    }
}
